package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Utente;

import sessions.UserManagerRemote;
import staticHtml.PaginaStatica;


public class RicercaUtentiServlet extends HttpServlet {

	public RicercaUtentiServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		cercaUtenti(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		cercaUtenti(request, response);
	}

	private void cercaUtenti(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");

		PrintWriter out = response.getWriter();
		
		Utente user= (Utente) request.getSession().getAttribute("utente");

		if(user==null)
		{
			out.println(PaginaStatica.top());
			out.println("Sessione scaduta<br><a href='index.html'> Torna alla home </a>");
			out.println(PaginaStatica.bottom());
			return;
		}
		out.println(PaginaStatica.topUser());

		String nome = request.getParameter("nome");
		String cognome = request.getParameter("cognome");
		if(nome == null || nome.length() <= 0 || cognome == null || cognome.length() <= 0) {
			out.println("Non lasciare vuoti i campi<a href='./ricercaUtenti.html'>Torna alla ricerca</a></body></html>");
			return;
		}
		if(!nome.matches("[^0-9]*")|| !cognome.matches("[^0-9]*")) {
			out.println("Non puoi inserire i numeri. <a href='./ricercaUtenti.html'>Torna alla ricerca</a></body></html>");
			return;
		}
		try {
			Hashtable<String,String> env = new Hashtable<String,String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			Object ref = jndiContext.lookup("UserManager/remote");
			UserManagerRemote r = (UserManagerRemote) ref;
			
			out.println("	<div id='titolo'>"+
					"<label class='titolo'> Risultati della ricerca</label></div>");
			
			List<Utente> utente = r.getUtenteDaNomeCognome(nome, cognome);
			if(utente == null || utente.isEmpty()) {
				out.println("Utente non trovato... <a href='./ricercaUtenti.html'>Torna alla ricerca</a>");
				out.println(PaginaStatica.bottom());
				return;
			}
			for(Utente u :utente)
			{
				if(u.getUserName().compareTo(user.getUserName())!=0){
					
					out.println("<form class ='risRicercaUtenti' action='./richiestaAmicizia.html' method='POST'>");
					out.println("<a href='./paginaPersonale.html?name="+u.getUserName()+"'>"+ u.getUserName()+"</a>    <label>  "+u.getNome()+"  "+u.getCognome()+"</label>");
					out.println("<input type='submit' value='richiedi amicizia'>");
					out.println("<input type=hidden name='destinatario' value="+u.getUserName()+">");
										
					out.println("</form>");
					
				}

			}



		} catch (NamingException e) {
			e.printStackTrace();
		}
		out.println(PaginaStatica.bottom());
	}
}